package com.Txunda.fxw.ui.activity.law;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.L;
import com.Txunda.fxw.base.utils.util.ListUtils;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.ui.activity.other.PathAty;
import com.Txunda.fxw.ui.adapter.BottomLawAdapter2;
import com.Txunda.fxw.ui.adapter.Law_DetailAdapter;
import com.Txunda.fxw.ui.adapter.model.LawModel;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.http.Office;
import com.Txunda.fxw.ui.toolkit.util.PreferenceUtil;
import com.Txunda.fxw.view.RoundedImageView;
import com.Txunda.fxw.view.dialog.AlertDialog;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;


/**
 * 律师事务所
 *
 * @author Administrator
 */
public class Law_firmAty extends BaseAty {

    @BindView(R.id.aty_title3_icon)
    ImageView atyTitle3Icon;
    @BindView(R.id.aty_title3_tv_center)
    TextView atyTitle3TvCenter;
    @BindView(R.id.aty_title3_tv_right2)
    TextView atyTitle3TvRight2;
    @BindView(R.id.law_recycler)
    RecyclerView lawRecycler;
    @BindView(R.id.tv_service)
    TextView tvService;
    @BindView(R.id.tv_shop_hours)
    TextView tvShopHours;
    @BindView(R.id.tv_tel)
    TextView tvTel;
    @BindView(R.id.tv_address_item)
    TextView tvAddressItem;
    @BindView(R.id.tv_address)
    RelativeLayout tvAddress;
    @BindView(R.id.tv_distance)
    TextView tvDistance;
    @BindView(R.id.real_distance)
    RelativeLayout realDistance;
    @BindView(R.id.tv_img)
    ImageView tvImg;
    @BindView(R.id.img_name_tv)
    TextView imgNameTv;
    @BindView(R.id.tv_name_wechat)
    TextView tvNameWechat;
    @BindView(R.id.linear_more_wechat)
    RelativeLayout linearMoreWechat;
    @BindView(R.id.bottom_detail_recycler)
    RecyclerView bottomDetailRecycler;
    @BindView(R.id.icon_logo)
    RoundedImageView iconLogo;
    @BindView(R.id.side_bar_phone_tv)
    LinearLayout sideBarPhoneTv;
    @BindView(R.id.side_bar_like_iv)
    ImageView sideBarLikeIv;
    @BindView(R.id.side_bar_like_tv)
    TextView sideBarLikeTv;
    @BindView(R.id.side_bar_like_layout)
    LinearLayout sideBarLikeLayout;
    @BindView(R.id.side_bar_evaluate_tv)
    LinearLayout sideBarEvaluateTv;
    @BindView(R.id.side_bar_settings_tv)
    LinearLayout sideBarSettingsTv;
    @BindView(R.id.side_bar_share_tv)
    LinearLayout sideBarShareTv;
    @BindView(R.id.app_side_top_layout)
    LinearLayout appSideTopLayout;
    @BindView(R.id.linear_share2)
    LinearLayout linearShare2;
    @BindView(R.id.linear_weibo)
    LinearLayout linearWeibo;
    @BindView(R.id.linear_weixin)
    LinearLayout linearWeixin;
    @BindView(R.id.linear_friends)
    LinearLayout linearFriends;
    @BindView(R.id.linear_qq)
    LinearLayout linearQq;
    @BindView(R.id.linear_bottom)
    LinearLayout linearBottom;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    private Office office;
    private BottomLawAdapter2 bottomAdapter;

    private String o_id;

    private ArrayList<Map<String, String>> law_district_list;

    private List<LawModel> list_data;
    private ArrayList<Map<String, String>> bottom_list_data;
    private String lat;
    private String lnt;
    private String pic;
    private String address;
    private String name;
    private String url;
    private String wechat_url;
    private Law_DetailAdapter adapter;
    private String district_id = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setDrawerLayout(drawerLayout);
        String area_name = PreferenceUtil.readString(getApplicationContext(), PreferenceUtil.sp_mode);
        String district_name = getIntent().getStringExtra("district_name");

        String district = PreferenceUtil.readString(getApplicationContext(), PreferenceUtil.sp_district);
        if (district_name == null || district_name.equals("")) {
            atyTitle3TvRight2.setText(area_name);
            showProgressDialog();
            office.office(district_id, district, this);
        } else {
            atyTitle3TvRight2.setText(district_name);
            showProgressDialog();
            office.office(district_id, district_name, this);
        }
        getLikesId(sideBarLikeIv, o_id);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_law_firm;
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
        if (MapUtils.isEmpty(map))
            return;
        if (requestUrl.contains("office")) { // 律师事务所
            if ("success".equals(map.get("flag"))) {
                Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                if (MapUtils.isEmpty(data))
                    return;
                name = data.get("name");
                url = data.get("url");
                atyTitle3TvCenter.setText(name);
                tvService.setText(data.get("service")); // 服务宗旨
                tvShopHours.setText(data.get("shop_hours")); // 营业时间
                tvTel.setText(data.get("tel"));
                tvAddressItem.setText(data.get("address")); // 地址
                tvDistance.setText(data.get("distance")); // 距离

                Glide.with(this).load(data.get("pic")).into(tvImg);
                imgNameTv.setText(data.get("pic_name"));

                wechat_url = data.get("wechat_url"); // 微信介绍链接
                String wechat = data.get("wechat");
                if (wechat == null || wechat.equals("")) {
                    linearMoreWechat.setVisibility(View.GONE);
                } else {
                    linearMoreWechat.setVisibility(View.VISIBLE);
                    tvNameWechat.setText(wechat);
                }
                String take_card = data.get("take_card"); // 取证
                String change_files = data.get("change_files"); // 调档
                String use_files = data.get("use_files"); // 组案卷
                String record = data.get("record"); // 立案
                String bid = data.get("bid"); // 投标
                String refer = data.get("refer"); // 咨询
                String write = data.get("write"); // 写文书
                String agency = data.get("agency"); // 代理
                o_id = data.get("o_id"); // 律师事务所的id
                LawModel lawModel;
                if ("1".equals(take_card)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_quzheng);
                    lawModel.setTitle("取证");
                    list_data.add(lawModel);
                }
                if ("1".equals(change_files)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_diaodang);
                    lawModel.setTitle("调档");
                    list_data.add(lawModel);
                }
                if ("1".equals(use_files)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_zuanjuan);
                    lawModel.setTitle("组案卷");
                    list_data.add(lawModel);
                }
                if ("1".equals(record)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_lian);
                    lawModel.setTitle("立案");
                    list_data.add(lawModel);
                }
                if ("1".equals(bid)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_toubiao);
                    lawModel.setTitle("投标");
                    list_data.add(lawModel);
                }
                if ("1".equals(refer)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_zixun);
                    lawModel.setTitle("咨询");
                    list_data.add(lawModel);
                }
                if ("1".equals(write)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_write_wenshu);
                    lawModel.setTitle("写文书");
                    list_data.add(lawModel);
                }
                if ("1".equals(agency)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_daili);
                    lawModel.setTitle("代理");
                    list_data.add(lawModel);
                }

                lat = data.get("lat"); // 纬度
                lnt = data.get("lnt"); // 经度
                pic = data.get("pic"); // 加载图片
                address = data.get("address"); // 地址
                adapter.setList(list_data);

                lawRecycler.setAdapter(adapter);

                bottom_list_data = JSONUtils.parseKeyAndValueToMapList(data.get("shop_list"));
                if (ListUtils.isEmpty(law_district_list))
                    return;
                if (bottom_list_data.size() == 1) {
                    Map<String, String> bld1 = new HashMap<>();
                    bld1.put("com_id", "");
                    bld1.put("class_id", "");
                    bld1.put("name", "");
                    bld1.put("type", "0");
                    bld1.put("o_id", "");
                    bld1.put("district_id", "");
                    Map<String, String> bld2 = new HashMap<>();
                    bld2.put("com_id", "");
                    bld2.put("class_id", "");
                    bld2.put("name", "");
                    bld2.put("type", "0");
                    bld2.put("o_id", "");
                    bld2.put("district_id", "");
                    bottom_list_data.add(0, bld2);
                    bottom_list_data.add(0, bld1);
                } else if (bottom_list_data.size() == 2) {
                    Map<String, String> bld3 = new HashMap<>();
                    bld3.put("com_id", "");
                    bld3.put("class_id", "");
                    bld3.put("name", "");
                    bld3.put("type", "0");
                    bld3.put("o_id", "");
                    bld3.put("district_id", "");
                    bottom_list_data.add(0, bld3);
                }

                bottomAdapter.setList(bottom_list_data);
                bottomDetailRecycler.setAdapter(bottomAdapter);

            }
        }
    }

    @Override
    protected void initialized() {
        district_id = getIntent().getStringExtra("district_id");
        type = "fs" + district_id;
        bottom_list_data = new ArrayList<>();
        list_data = new ArrayList<>();
        office = new Office();
        String logo = PreferenceUtil.readString(getApplicationContext(), PreferenceUtil.sp_logo);
        Glide.with(this).load(logo).into(iconLogo);
        law_district_list = new ArrayList<>();

        atyTitle3TvRight2.setVisibility(View.VISIBLE);
        initRecycleView();
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecycleView() {
        // 设置LinearLayoutManager
        lawRecycler.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
        lawRecycler.setItemAnimator(new DefaultItemAnimator());
        // 设置固定大小
        lawRecycler.setHasFixedSize(true);
        adapter = new Law_DetailAdapter(Law_firmAty.this);

        // 为底部的recyclerView设置LinearManager管理器对象
        bottomDetailRecycler.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
        bottomDetailRecycler.setItemAnimator(new DefaultItemAnimator());
        // 设置固定大小
        bottomDetailRecycler.setHasFixedSize(true);
        bottomAdapter = new BottomLawAdapter2(Law_firmAty.this);
    }

    @Override
    @OnClick({R.id.icon_logo, R.id.real_distance, R.id.tv_address, R.id.tv_img, R.id.tv_tel,
            R.id.linear_more_wechat, R.id.side_bar_share_tv, R.id.linear_share2})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.linear_more_wechat:
                try {
                    Uri uri = Uri.parse(wechat_url);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.tv_tel:
                try {
                    new AlertDialog(this).builder().setTitle("提示").setMsg("tel: " + tvTel.getText().toString())
                            .setPositiveButton("确认", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(Intent.ACTION_CALL,
                                            Uri.parse("tel: " + tvTel.getText().toString()));
                                    startActivity(intent);
                                }
                            }).setNegativeButton("取消", new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                        }
                    }).show();

                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.tv_img: // 图片链接跳转
                try {
                    Uri uri = Uri.parse(url);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                } catch (Exception e) {
                    L.e(e.getMessage());
                }
                break;
            case R.id.tv_address:
                Bundle bundle = new Bundle();
                bundle.putString("lat", lat);
                bundle.putString("lnt", lnt);
                bundle.putString("address", address);
                bundle.putString("name", name);
                bundle.putString("pic", pic);
                startActivity(PathAty.class, bundle);
                break;
            case R.id.icon_logo: // logo
                if (!drawerLayout.isDrawerOpen(Gravity.END))
                    drawerLayout.openDrawer(Gravity.END);
                appSideTopLayout.setVisibility(View.VISIBLE);
                linearBottom.setVisibility(View.GONE);
                break;
            case R.id.real_distance: // 距离
                bundle = new Bundle();
                bundle.putString("o_id", o_id);
                startActivity(Coutrt_ColumnAty.class, bundle); // 法院专栏
                break;
            case R.id.side_bar_share_tv:
                appSideTopLayout.setVisibility(View.GONE);
                linearBottom.setVisibility(View.VISIBLE);
                break;
            case R.id.linear_share2:
                appSideTopLayout.setVisibility(View.VISIBLE);
                linearBottom.setVisibility(View.GONE);
                break;

            default:
                break;
        }
    }

    @Override
    protected void requestData() {
    }

    /**
     * 律师地区筛选adapter
     *
     * @author Administrator
     */
    public class MyAdapter extends BaseAdapter {
        List<Map<String, String>> list;
        MyViewHolder vh;

        public MyAdapter(List<Map<String, String>> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            int ret = 0;
            if (list != null) {
                ret = list.size();
            }
            return ret;
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(Law_firmAty.this).inflate(R.layout.law_item_slide, parent, false);
                vh = new MyViewHolder();
                ViewUtils.inject(vh, convertView);
                convertView.setTag(vh);
            } else {
                vh = (MyViewHolder) convertView.getTag();
            }
            vh.item_slide_law.setText(list.get(position).get("district_id")); // 地区名称

            return convertView;
        }

        class MyViewHolder {
            @ViewInject(R.id.item_slide_law)
            private TextView item_slide_law;

        }

    }

}
